package com.ia.aistream.manager.system.impl;

import com.ia.aistream.common.utils.StringUtils;
import com.ia.aistream.manager.system.ISysDictTypeManager;
import com.ia.aistream.manager.system.utils.DictUtils;
import com.ia.aistream.mapper.system.SysDictDataMapper;
import com.ia.aistream.mapper.system.SysDictTypeMapper;
import com.ia.aistream.model.system.entity.SysDictData;
import com.ia.aistream.model.system.entity.SysDictType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class SysDictTypeManagerImpl implements ISysDictTypeManager {

    @Autowired
    private SysDictDataMapper dictDataMapper;
    @Autowired
    private SysDictTypeMapper dictTypeMapper;

    @Override
    public List<SysDictData> selectDictDataByType(String dictType) {
        List<SysDictData> dictDatas = DictUtils.getDictCache(dictType);
        if (StringUtils.isNotNull(dictDatas))
        {
            return dictDatas;
        }
        dictDatas = dictDataMapper.selectDictDataByType(dictType);
        if (StringUtils.isNotNull(dictDatas))
        {
            DictUtils.setDictCache(dictType, dictDatas);
            return dictDatas;
        }
        return null;
    }

    @Override
    public void init() {
        List<SysDictType> dictTypeList = dictTypeMapper.selectDictTypeAll();
        for (SysDictType dictType : dictTypeList)
        {
            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(dictType.getDictType());
            DictUtils.setDictCache(dictType.getDictType(), dictDatas);
        }
    }
}
